package com.hxd.base;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import io.swagger.annotations.ApiOperation;

import com.hxd.common.Page;
import com.hxd.common.Result;

import static com.hxd.common.Result.*;

import java.util.List;

/**
 * 基础控制器
 * @author zhoujw
 *
 * @param <S> Service
 * @param <T> Bean
 */
public class BaseController<T extends BaseEntity, V> {
	
	protected final Logger logger = LoggerFactory.getLogger(BaseController.class);
	
	@Autowired
	private BaseService<T, V> service;
	
	@PostMapping("/list")
	@ApiOperation(value = "查询列表")
	public Result list(@RequestBody T entity) {
		return success(service.page(new Page<T>(entity)));
	}
	
	@PostMapping("/detail")
	@ApiOperation(value = "查询详情")
	public Result getById(@RequestParam Integer id) {
		return success(service.getById(id));
	}
	
	@PostMapping("/save")
	@ApiOperation(value = "保存数据")
	public Result save(@RequestBody V vo) {
		return success(service.saveOrUpdate(vo, "id"));
	}
	
	@PostMapping("/update")
	@ApiOperation(value = "更新数据")
	public Result update(@RequestBody V vo) {
		return success(service.saveOrUpdate(vo));
	}
	
	@PostMapping("/delete")
	@ApiOperation(value = "批量删除数据")
	public Result delete(@RequestBody List<Integer> ids) {
		return success(service.removeByIds(ids));
	}
	
}
